package com.wbdgj.ui.mine;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.internal.LinkedTreeMap;
import com.google.zxing.WriterException;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.base.BaseWhiteActivity;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.home.ZXingActivity;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.utils.QRCodeUtil;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;

import java.text.DecimalFormat;

import butterknife.BindView;
import retrofit2.Call;

/**
 * Created by Administrator on 2018/6/1.
 */

public class MyEwmActivity extends BaseWhiteActivity{

    @BindView(R.id.ewmImg)
    ImageView ewmImg;

    @BindView(R.id.txmImg)
    ImageView txmImg;
    @BindView(R.id.yue)
    TextView yue;

    @BindView(R.id.NAME)
    TextView NAME;
    @BindView(R.id.PT_CARD)
    TextView PT_CARD;

    private Context context = this;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_my_ewm;
    }

    @Override
    protected void initView() {
        getMbUserInfo();

        findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    private void getMbUserInfo() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getMbUserInfo(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if (linkedTreeMap.containsKey("LEVEL")){
                        if (decimalFormat.format(linkedTreeMap.get("LEVEL")).equals("1")){
                            yue.setText("您是普通会员哦！");
                        }else if (decimalFormat.format(linkedTreeMap.get("LEVEL")).equals("2")){
                            yue.setText("您是黄金会员哦！");
                        }else if (decimalFormat.format(linkedTreeMap.get("LEVEL")).equals("3")){
                            yue.setText("您是白金会员哦！");
                        }else if (decimalFormat.format(linkedTreeMap.get("LEVEL")).equals("4")){
                            yue.setText("您是钻石会员哦！");
                        }else if (decimalFormat.format(linkedTreeMap.get("LEVEL")).equals("5")){
                            yue.setText("您是黑钻会员哦！");
                        }else if (decimalFormat.format(linkedTreeMap.get("LEVEL")).equals("6")){
                            yue.setText("您是股东会员哦！");
                        }else{
                            yue.setText("您是普通会员哦！");
                        }
                        NAME.setText(linkedTreeMap.get("NAME")+"");
                        PT_CARD.setText("会员卡号："+linkedTreeMap.get("PT_CARD")+"");
//                        Glide.with(context).load(HttpAdapter.HEAD_URL+mDataList.get(position).get("LOGO")).placeholder(R.mipmap.ls_wait).error(R.mipmap.ls_error).into(holder.image);
                        try {
                            // 根据字符串生成二维码图片并显示在界面上，第二个参数为图片的大小（310*310）
                            Bitmap bitmapQRCode = QRCodeUtil.createQRCode(linkedTreeMap.get("ID_CARD")+"",
                                    510);
                            ewmImg.setImageBitmap(bitmapQRCode);
                        } catch (WriterException e) {
                            e.printStackTrace();
                        }
                    }
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

    private void queryPayCode() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().appmbpaycodequery(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();



                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }
}
